package router

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/go-zero/rest"
	"net/http"
	"payme3000/api_gateway/internal/config"
	"payme3000/framework/base"
	"payme3000/pb"
)

const RoutePrefixAuth = "/api"

type DefaultRouter struct {
	BaseRouter

	cfg *config.Config
}

func NewDefaultRouter(
	cfg *config.Config,
) *DefaultRouter {
	return &DefaultRouter{
		cfg: cfg,
	}
}

func (r *DefaultRouter) Groups() []base.RouteGroup {
	return []base.RouteGroup{
		{
			Routes: rest.WithMiddlewares(
				[]rest.Middleware{},
				[]rest.Route{
					{
						Method:  http.MethodGet,
						Path:    "/ping",
						Handler: r.Ping,
					},
					{
						Method:  http.MethodPost,
						Path:    "/ping",
						Handler: r.Ping,
					},
				}...,
			),
			Options: []rest.RouteOption{
				rest.WithPrefix(RoutePrefixAuth),
			},
		},
	}
}

func (r *DefaultRouter) Ping(w http.ResponseWriter, hr *http.Request) {
	req := &pb.PingREQ{}
	r.Handle(w, hr, req, func(ctx context.Context) (any, error) {
		logx.Infof("req: %+v", req)
		rsp := &pb.PingRSP{
			Code: 1,
		}

		return rsp, nil
	})
}

func (r *DefaultRouter) Confirm(w http.ResponseWriter, hr *http.Request) {
	req := &pb.PingREQ{}
	r.Handle(w, hr, req, func(ctx context.Context) (any, error) {
		logx.Infof("req: %+v", req)
		rsp := &pb.PingRSP{
			Code: 1,
		}

		return rsp, nil
	})
}
